﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using FC_MFDs.Properties;

namespace FC_MFDs
{
    public partial class RightMFCD : Form
    {
        public RightMFCD()
        {
            InitializeComponent();

            // Necessary to take the window frame width/height into account
            chromeWidth = Width - ClientSize.Width;
            chromeHeight = Height - ClientSize.Height;
        }

        private void RightMFCDLoad(object sender, EventArgs e)
        {
            // Set window location
            if (Settings.Default.rightMFCDPos != null)
            {
                this.Location = Settings.Default.rightMFCDPos;
            }

            // Set window size
            if (Settings.Default.rightMFCDSize != null)
            {
                this.Size = Settings.Default.rightMFCDSize;
            }

            this.TopMost = true;
        }

        private void RightMFCDClosing(object sender, FormClosingEventArgs e)
        {
            // Copy window location to app settings
            Settings.Default.rightMFCDPos = this.Location;

            // Copy window size to app settings
            if (this.WindowState == FormWindowState.Normal)
            {
                Settings.Default.rightMFCDSize = this.Size;
            }
            else
            {
                Settings.Default.rightMFCDSize = this.RestoreBounds.Size;
            }

            // Save settings
            Settings.Default.Save();

        }

        #region Resizer
        private float constantWidth = 1;
        private float constantHeight = 1;

        private int chromeWidth;
        private int chromeHeight;

        // From Windows SDK
        private const int WM_SIZING = 0x214;

        private const int WMSZ_LEFT = 1;
        private const int WMSZ_RIGHT = 2;
        private const int WMSZ_TOP = 3;
        private const int WMSZ_BOTTOM = 6;

        struct RECT
        {
            public int Left;
            public int Top;
            public int Right;
            public int Bottom;
        }

        protected override void WndProc(ref Message m)
        {
            if (m.Msg == WM_SIZING)
            {
                RECT rc = (RECT)Marshal.PtrToStructure(m.LParam, typeof(RECT));

                int w = rc.Right - rc.Left - chromeWidth;
                int h = rc.Bottom - rc.Top - chromeHeight;

                switch (m.WParam.ToInt32()) // Resize handle
                {
                    case WMSZ_LEFT:
                    case WMSZ_RIGHT:
                        // Left or right handles, adjust height
                        rc.Bottom = rc.Top + chromeHeight + (int)(constantHeight * w / constantWidth);
                        break;

                    case WMSZ_TOP:
                    case WMSZ_BOTTOM:
                        // Top or bottom handles, adjust width
                        rc.Right = rc.Left + chromeWidth + (int)(constantWidth * h / constantHeight);
                        break;

                    case WMSZ_LEFT + WMSZ_TOP:
                    case WMSZ_LEFT + WMSZ_BOTTOM:
                        // Top-left or bottom-left handles, adjust width
                        rc.Left = rc.Right - chromeWidth - (int)(constantWidth * h / constantHeight);
                        break;

                    case WMSZ_RIGHT + WMSZ_TOP:
                        // Top-right handle, adjust height
                        rc.Top = rc.Bottom - chromeHeight - (int)(constantHeight * w / constantWidth);
                        break;

                    case WMSZ_RIGHT + WMSZ_BOTTOM:
                        // Bottom-right handle, adjust height
                        rc.Bottom = rc.Top + chromeHeight + (int)(constantHeight * w / constantWidth);
                        break;
                }

                Marshal.StructureToPtr(rc, m.LParam, true);
            }

            base.WndProc(ref m);
        }
        #endregion

    }
}
